package TrafficControlSystem.AlternativeLights;

import java.awt.BorderLayout;
import java.awt.EventQueue;

import javax.swing.JFrame;
import javax.swing.JOptionPane;
import javax.swing.JPanel;
import javax.swing.border.EmptyBorder;
import java.awt.FlowLayout;
import javax.swing.JButton;
import javax.swing.JCheckBox;

import TrafficControlSystem.AlternativeLights.LightsController.Mode;
import TrafficControlSystem.Common.IntensityInfo.Direction;

import java.awt.GridLayout;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import java.util.Observable;
import java.util.Observer;

public class TrafficLightsFrame extends JFrame implements Observer {

	/**
	 * 
	 */
	private static final long serialVersionUID = 1L;

	private JPanel contentPane;

	private final LightsController controller; // kontroler do ktorego wysylane sa eventy
	
	private CrossingPanel crossingPanels[];
	private JButton btnConnect;	
	final private  JCheckBox automaticControl;
	
	/**
	 * Create the frame.
	 */
	public TrafficLightsFrame(final LightsController controller) {
		
		this.controller = controller;
		
		controller.addObserver(this);
		
		setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
		setBounds(100, 100, 450, 300);
		contentPane = new JPanel();
		contentPane.setBorder(new EmptyBorder(5, 5, 5, 5));
		setContentPane(contentPane);
		contentPane.setLayout(new BorderLayout(0, 0));
		
		JPanel controlPanel = new JPanel();
		contentPane.add(controlPanel, BorderLayout.NORTH);
		controlPanel.setLayout(new FlowLayout(FlowLayout.CENTER, 5, 5));
		
		btnConnect = new JButton("Polacz");
		controlPanel.add(btnConnect);
		btnConnect.addActionListener(new ActionListener() {
			@Override
			public void actionPerformed(ActionEvent e) {
				
				String ip = JOptionPane.showInputDialog("Podaj IP serwera:", controller.getDefaultHost());
				
				if(ip==null)
					return;
				
				System.out.println("Proba polaczenia do " + ip + ":" + Integer.toString(controller.getDefaultPort()));
				
				if(!controller.connect(ip, controller.getDefaultPort())){
					JOptionPane.showMessageDialog(TrafficLightsFrame.this, "Nie udalo sie polaczyc!");
				} else {
					JOptionPane.showMessageDialog(TrafficLightsFrame.this, "Polaczono!");
				}
				
				
			}
		});
		
		automaticControl = new JCheckBox("Automatyczne sterowanie");
		controlPanel.add(automaticControl);
		automaticControl.addActionListener(new ActionListener() {
			@Override
			public void actionPerformed(ActionEvent e) {
				if(automaticControl.isSelected()){
					// wlaczenie automatu
					controller.setMode(Mode.AUTOMATIC);
				}else{
					// wlaczenei trybu manualnego
					controller.setMode(Mode.MANUAL);
				}
				
			}
		});
		
		JPanel basecrossingsPanel = new JPanel();
		contentPane.add(basecrossingsPanel, BorderLayout.CENTER);
		basecrossingsPanel.setLayout(new GridLayout(3, 3, 0, 0));

		crossingPanels = new CrossingPanel[9];
		for(int i=0; i<crossingPanels.length; i++)
		{
			crossingPanels[i] = new CrossingPanel(controller, i);
			basecrossingsPanel.add(crossingPanels[i]);
		}
		
		this.pack();

		
	}

	@Override
	public void update(Observable o, Object arg) {
		
		if(o==controller)
		{
			
			//System.out.println("Informed :)");
			
			// Przycisk "polacz" oraz ogolnie panele
			btnConnect.setEnabled(!controller.isConnected());
			
			for(int i=0; i<crossingPanels.length; i++)
			{
				crossingPanels[i].setLightsState(controller.getLightsState(i));
			}
			
			// Intensywnosc ruchu
			if(controller.hasIntensityInfo())
				for(int i=0; i<crossingPanels.length; i++)
				{
					int h = controller.getIntensity(i, Direction.HORIZONTAL);
					int v = controller.getIntensity(i, Direction.VERTICAL);
					crossingPanels[i].setIntensityInfo(h,v);
				}
			
			// Swiatla
			for(int i=0; i<crossingPanels.length; i++)
			{
				crossingPanels[i].setLightsState(controller.getLightsState(i));
			}
			
			// Przyciski sterowania manualnego
			for(int i=0; i<crossingPanels.length; i++)
			{
				crossingPanels[i].setManualChangingEnabled(controller.getMode()==Mode.MANUAL);
			}			
			
			// Tryb sterowania
			automaticControl.setSelected(controller.getMode()==Mode.AUTOMATIC);
			
			
			
		}
		
		
		
	}
	
	
}
